package com.lw.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lw.commonutil.result.ResultCodeEnum;
import com.lw.commonutil.utils.JwtUtils;
import com.lw.commonutil.utils.MD5;
import com.lw.servicebase.exception.GuliException;
import com.lw.ucenter.entity.UcenterMember;
import com.lw.ucenter.entity.vo.RegisterVo;
import com.lw.ucenter.mapper.UcenterMemberMapper;
import com.lw.ucenter.service.UcenterMemberService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper,UcenterMember> implements UcenterMemberService{

    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public String login(UcenterMember ucenterMember) {
        //判断手机号和密码是否为空
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
        if (StringUtils.isEmpty( mobile )||StringUtils.isEmpty( password )){
            throw new GuliException( ResultCodeEnum.FAIL );
        }
        //根据手机号查询
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq( "mobile",mobile );
        UcenterMember member = baseMapper.selectOne( wrapper );
        if (member==null){
            throw new GuliException( ResultCodeEnum.FAIL );
        }
        //判断是否禁用
        if (member.getIsDisabled()==1){
            throw new GuliException( ResultCodeEnum.FAIL );
        }
        //判断密码是否正确，加密比较
        String MDPassword = MD5.encrypt( password );
        if (member.getPassword().equals( MDPassword )){
            //生成token
            String token = JwtUtils.getJwtToken( member.getId(), member.getNickname() );
            return token;
        }else {
            return null;
        }
    }

    //注册接口
    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String nickname = registerVo.getNickname();
        //非空判断
        if (StringUtils.isEmpty( code )||StringUtils.isEmpty( mobile )||StringUtils.isEmpty( password )||StringUtils.isEmpty( nickname )){
            throw new GuliException( ResultCodeEnum.FAIL );
        }
        //判断验证码是否相同
        String redisCode = (String) redisTemplate.opsForValue().get( mobile );
        if (!redisCode.equals( code )){
            throw new GuliException( ResultCodeEnum.FAIL );
        }
        //判断手机号是否已注册
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq( "mobile",mobile );
        Integer count = baseMapper.selectCount( wrapper );
        if (count>0){
            throw new GuliException( ResultCodeEnum.FAIL );
        }
        //数据添加到数据库
        UcenterMember member = new UcenterMember();
        member.setNickname( nickname );
        member.setMobile( mobile );
        member.setIsDisabled( 0 );
        member.setPassword( MD5.encrypt( password ) );
        member.setAvatar( "" );
        baseMapper.insert( member );
    }

    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
